package com.iplant.basic.serviceImpl.dao.crm;

import com.iplant.basic.service.po.crm.CRMCustomerType;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class CRMCustomerTypeDAO extends BaseDAO {

    private static CRMCustomerTypeDAO Instance = null;
    private static Logger logger = LoggerFactory.getLogger(CRMCustomerTypeDAO.class);

    private CRMCustomerTypeDAO() {
        super();

        setTableName("mss_batchrule");
    }

    public static CRMCustomerTypeDAO getInstance() {
        if (Instance == null)
            Instance = new CRMCustomerTypeDAO();
        return Instance;
    }

    public void CRM_UpdateCustomerType(BMSEmployee wLoginUser, CRMCustomerType wCustomerType, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);

        try {
            if (wCustomerType == null || StringUtils.isEmpty(wCustomerType.Name)) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }
            CRMCustomerType wCustomerTypeO = this.CRM_CheckCustomerType(wLoginUser, wCustomerType, wErrorCode);
            if (wCustomerTypeO.ID > 0) {
                wErrorCode.set(ExceptionTypes.Duplication.getValue());
            }
            if (wErrorCode.Result != 0)
                return;

            Map<String, Object> wParams = new HashMap<String, Object>();

            wParams.put("Name", wCustomerType.Name);
            wParams.put("Code", wCustomerType.Code);
            wParams.put("Description", wCustomerType.Description);

            wParams.put("EditorID", wLoginUser.getID());
            wParams.put("EditTime", Calendar.getInstance());


            if (wCustomerType.ID > 0) {
                wParams.put("ID", wCustomerType.ID);
                super.Update(super.getTableName("mss_batchrule"), "ID", wParams);
            } else {
                wParams.put("CreatorID", wLoginUser.getID());
                wParams.put("CreateTime", Calendar.getInstance());
                wParams.put("Active", 0);
                wCustomerType.ID = super.Insert(super.getTableName("mss_batchrule"), wParams);

            }

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("CRM_UpdateCustomerType", ex);
        }
    }

    public void CRM_DeleteCustomerType(BMSEmployee wLoginUser, CRMCustomerType wCustomerType, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);

        try {
            if (wCustomerType == null || wCustomerType.ID <= 0) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }
            this.DeletePO(wLoginUser, wCustomerType, wErrorCode);


        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("CRM_UpdateCustomerType", ex);
        }
    }


    public void CRM_ActiveCustomerType(BMSEmployee wLoginUser, CRMCustomerType wCustomerType, int wActive,
                                       OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);
        // 判断客户信息是否存在(中国：统一社会信用代码，国外:提醒是否有重复）

        try {
            if (wCustomerType == null || wCustomerType.ID <= 0) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }

            Map<String, Object> wParams = new HashMap<String, Object>();

            wParams.put("ID", wCustomerType.ID);
            wParams.put("EditorID", wLoginUser.getID());
            wParams.put("EditTime", Calendar.getInstance());
            wParams.put("Active", wActive);
            super.Update(super.getTableName("mss_batchrule"), "ID", wParams);

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("CRM_ActiveCustomerType", ex);
        }
    }

    public List<CRMCustomerType> CRM_QueryCustomerTypeList(
            BMSEmployee wLoginUser, String wName,
            int wActive,
            Pagination wPagination, OutResult<Integer> wErrorCode) {
        return this.CRM_QueryCustomerTypeList(wLoginUser, -1, "", wName,
                wActive, wPagination, wErrorCode);
    }

    private List<CRMCustomerType> CRM_QueryCustomerTypeList(
            BMSEmployee wLoginUser, int wID, String wCode, String wName,
            int wActive, Pagination wPagination, OutResult<Integer> wErrorCode) {
        List<CRMCustomerType> wCRMCustomerTypeList = new ArrayList<CRMCustomerType>();
        wErrorCode.set(0);

        try {

            // Step02:查询
            Map<String, Object> wParams = new HashMap<String, Object>();
            String wSQLText = "";
            wSQLText = StringUtils.Format(
                    "Select t.*,t3.Name as CreatorName ,t4.Name as EditorName  from {0}.mss_batchrule t  "

                            + " left join {0}.mbs_user t3 on t.CreatorID=t3.ID "
                            + " left join {0}.mbs_user t4 on t.EditorID=t4.ID "
                            + " where (:ID<=0 or  t.ID=:ID) and (:Code =''''  or  t.Code=:Code) "
                            + " AND (:Active<0 or  t.Active=:Active) AND (:Name = '''' or  t.Name like :Name) ",
                    super.getDBName());

            wSQLText = this.DMLChange(wSQLText);

            wParams.put("ID", wID);
            wParams.put("Code", wCode);
            wParams.put("Active", wActive);
            wParams.put("Name", StringUtils.isEmpty(wName) ? "" : ("%" + wName + "%"));

            wCRMCustomerTypeList = super.QueryForList(wSQLText, wParams, wPagination, CRMCustomerType.class);

            // Step03:配置系统
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("CRM_QueryCustomerTypeList", ex);
        }
        return wCRMCustomerTypeList;
    }

    public CRMCustomerType CRM_QueryCustomerType(BMSEmployee wLoginUser, int wID, String wCode, OutResult<Integer> wErrorCode) {
        CRMCustomerType wResult = new CRMCustomerType();
        wErrorCode.set(0);

        try {
            List<CRMCustomerType> wList = null;
            if (wID > 0) {
                wList = this.CRM_QueryCustomerTypeList(wLoginUser, wID, "", "", -1, Pagination.getDefault(), wErrorCode);

            } else if (StringUtils.isNotEmpty(wCode)) {
                wList = this.CRM_QueryCustomerTypeList(wLoginUser, -1, wCode, "", -1, Pagination.getDefault(), wErrorCode);
            }
            if (wList != null && wList.size() > 0) {
                wResult = wList.get(0);
            }
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("CRM_QueryCustomerTypeByID", ex);
        }
        return wResult;
    }

    public CRMCustomerType CRM_CheckCustomerType(BMSEmployee wLoginUser, CRMCustomerType wCRMCustomerType, OutResult<Integer> wErrorCode) {
        CRMCustomerType wCustomerType = new CRMCustomerType();
        wErrorCode.set(0);

        try {

            // Step0:查询
            Map<String, Object> wParams = new HashMap<String, Object>();
            String wSQLText = StringUtils.Format(
                    "Select * from {0}.mss_batchrule "
                            + " where ID!=:ID " +
                            "  AND  Name =:Name or (:Code != '''' and Code=:Code) ",
                    super.getDBName());
            wParams.clear();
            wParams.put("ID", wCRMCustomerType.ID);
            wParams.put("Name", wCRMCustomerType.Name);
            wParams.put("Code", wCRMCustomerType.Code);

            wSQLText = this.DMLChange(wSQLText);
            List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParams);
            for (Map<String, Object> wSqlDataReader : wQueryResultList) {
                wCustomerType.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
                wCustomerType.Name = StringUtils.parseString(wSqlDataReader.get("Name"));
                wCustomerType.Code = StringUtils.parseString(wSqlDataReader.get("Code"));

                wCustomerType.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
                wCustomerType.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
                wCustomerType.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
                wCustomerType.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
                wCustomerType.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));

            }

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("CRM_CheckCustomerType", ex);
        }
        return wCustomerType;
    }

}
